﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace BPP
{
    public partial class CompoundData : Form
    {
        public CompoundData(string compound, _programm.Metabolites main_compound, ShowSelectedPath _showPathForm)
        {
            InitializeComponent();


            LoadingData pleaseWait = new LoadingData();

            pleaseWait.TopMost = true;
            pleaseWait.StartPosition = FormStartPosition.CenterScreen;
            pleaseWait.Show();

            fillMainCompoundInformation(main_compound);

            afterLaunch(compound, pleaseWait);

            pleaseWait.Close();

            showPathForm = _showPathForm;
        }

        ShowSelectedPath showPathForm;

        private void fillMainCompoundInformation(_programm.Metabolites main_compound)
        {

            txtCPDLink.Text = main_compound.Link;
            txtCPDName.Text = main_compound.Name;
            txtCPDFormula.Text = main_compound.Formula_charged;
            txtCPDFormulaNeutral.Text = main_compound.Formula_neutral;
            txtCPDCharge.Text = main_compound.Charge;
            txtCPDCompartment.Text = main_compound.Compartment;
            txtCPDKeggId.Text = main_compound.KEGG_ID;
            txtCPDChebiId.Text = main_compound.ChEBI_ID;
            txtCPDPubChemId.Text = main_compound.PubChem_ID;
            txtCPDInchi.Text = main_compound.InChI;
            txtCPDSmiles.Text = main_compound.SMILES;
            txtCPDAbbreviation.Text = main_compound.Abbreviation;
  

        }

        List<_programm.chebiObject> _chebi;

        private void afterLaunch(string compound, LoadingData form)
        {
            DateTime startTime = DateTime.Now;
            form.label1.Text = "Loading KEGG data for compound " + compound.Substring(4) + "\n" + "time: " + startTime;
            form.Refresh();

            _programm.keggObject _kegg = _programm.shared_functions.GetKeggDataByCompound(compound.Substring(4));

            startTime = DateTime.Now;
            form.label1.Text = "Loading Chebi data for compound " + compound.Substring(4) + "\n" + "time: " + startTime;
            form.Refresh();

            //fill chebi object
            _chebi = _programm.shared_functions.GetChebiDataByKeggCompound(compound.Substring(4)); //GetChebiDataByKeggCompound(compound.Substring(4));

            startTime = DateTime.Now;
            form.label1.Text = "Loading PubChem data for compound " + compound.Substring(4) + "\n" + " time: " + startTime;
            form.Refresh();

            _programm.pubChemObject _pubchem = _programm.shared_functions.GetPubChemDataByKeggCompound(compound.Substring(4));

            form.Refresh();


 ///////////////////////////////////////////////////////////////////////////       
           
                

            foreach (string s in _kegg.formula)
                txtKeggFormula.Text += s+"; ";

            //txtKeggInfo.Text = "";

            txtKeggKeggId.Text = _kegg.keggID;
            txtKeggLink.Text = _kegg.link;


            txtKeggName.Text = _kegg.Name ?? _kegg.synonimus[0];
           
       
        
            txtKeggChebiId.Text = _kegg.chebiID;
            txtKeggPubchemId.Text = _kegg.pubchemID;

            //foreach(string s in _kegg.reactions)
            //    txtKeggReactions.Text+= s+" ";

            foreach (string s in _kegg.synonimus)
                txtKeggName.Text += s + " ";

/////////////////////////////////////////////////////////////////////////////

            txtPubchemCharge.Text= _pubchem.charge;
            txtPubchemChebiId.Text = _pubchem.chebiID;

            foreach (string s in _pubchem.formula)
                txtPubchemFormula.Text += s + " ";
            txtPubchemInchi.Text = _pubchem.inchi;
            //txtPubchemInfo.Text = _pubchem.additionalInfo; ;
            txtPubchemKeggId.Text = _pubchem.keggID; ;
            txtPubchemLink.Text = _pubchem.link; ;
            txtPubchemName.Text = _pubchem.Name; ;
            txtPubchemPubchemId.Text = _pubchem.pubchemID; ;
           // txtPubchemReactions.Text = _pubchem.; ;
            txtPubchemSmiles.Text = _pubchem.smiles; ;
            foreach (string s in _pubchem.synonimus)
                txtPubchemName.Text += s + " ";



            if (_chebi.Count > 1)
            {
                lblChebiManyObjects.Text = "Many CHEBI objects found. Please select";

                foreach (var tmp in _chebi)
                    comboBox1.Items.Add(tmp.Name);
                comboBox1.SelectedIndex = 0;

                fillChebiData(comboBox1.SelectedIndex); 
            }
            else
            {
                comboBox1.Enabled = false;

                fillChebiData(0);
            }
        }

        private void fillChebiData( int index)
        {
            txtChebiCharge.Text = _chebi[index].charge;
            txtChebiChebiId.Text = _chebi[index].chebiID;

            foreach (string s in _chebi[index].formula)
                txtChebiFormula.Text += s + " ";
            txtChebiInchi.Text = _chebi[index].inchi;
            //foreach (string s in _chebi[comboBox1.SelectedIndex].secondaryChebiID)
            //    txtChebiInfo.Text += s + " ";
            txtChebiKeggId.Text = _chebi[index].keggID;
            txtChebiLink.Text = _chebi[index].link;
            txtChebiName.Text = _chebi[index].Name;
            
            txtChebiPubChemId.Text = "NO INFO";

            txtChebiSmiles.Text = _chebi[index].smiles;

            foreach (string s in _chebi[index].synonimus)
                txtChebiName.Text += s + " ";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            txtChebiCharge.Text = "";
            txtChebiChebiId.Text = "";
            txtChebiFormula.Text += "";
            txtChebiInchi.Text = "";
            txtChebiKeggId.Text = "";
            txtChebiLink.Text = "";
            txtChebiName.Text = "";
            txtChebiPubChemId.Text = "";
            txtChebiSmiles.Text = "";
            txtChebiName.Text = "";

            fillChebiData(comboBox1.SelectedIndex);       
   
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            //vinesti v klass metabolita finkciju shranitj
            //SaveToDB

            //proveritj cto bi odinakovoe raspoliozenie bilo bi
            string[] tmp = new string[12] {
                txtCPDAbbreviation.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDName.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDFormula.Text.Replace("'","").TrimStart().TrimEnd(),  
                txtCPDFormulaNeutral.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDCharge.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDCompartment.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDKeggId.Text.Replace("'","").TrimStart().TrimEnd(),   
                txtCPDPubChemId.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDChebiId.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDInchi.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDSmiles.Text.Replace("'","").TrimStart().TrimEnd(), 
                txtCPDLink.Text.Replace("'","").TrimStart().TrimEnd(),   
            };


            _programm.Metabolites metabolite = new _programm.Metabolites(tmp);

            metabolite.SaveToDb();

            MessageBox.Show("Compound information saved");

            showPathForm.loadGridViewData();

        }


    }
}
